﻿// Decompiled with JetBrains decompiler
// Type: CreateMfgByCoordinates.CApCreDialogManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.CreateWeldPoint;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.DataTypes;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Planning;


namespace CreateMfgByCoordinates;

internal class CApCreDialogManager
{
  private TxFrame m_tempFrame;
  public CApCreateWpCommonValidator PartValidator = new CApCreateWpCommonValidator();

  public TxCompoundPart WeldContainer { set; get; }

  public ITxWeldOperation WeldOperation { set; get; }

  public CApCreDialogManager()
  {
    this.PartValidator.AllowedTypes = new List<Type>()
    {
      typeof (TxPlanningPartInstance)
    };
    this.PartValidator.ErrorMessage = new ResourceManager("DnProcessSimulateCommands.CreateMfgByCoordinates.StringTable", this.GetType().Assembly).GetString("INVALID_PART");
  }

  public void CreateMfgByCoordinates(
    string mfgName,
    TxTransformation transformation,
    ITxProcessModelObject part)
  {
    ITxWeldPointCreationData pointCreationData = TxMfgCreationDataFactory.CreateWeldPointCreationData(mfgName, transformation.Translation);
    ITxWeldLocationOperationCreation operationCreation;
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      operationCreation = (ITxWeldLocationOperationCreation) this.WeldOperation;
      if (TxCompoundPart.op_Inequality(this.WeldContainer, (ITxObject) null))
        pointCreationData = (ITxWeldPointCreationData) TxMfgCreationDataFactory.CreateTcExposedWeldPointCreationData(pointCreationData, TxMfgCreationDataFactory.CreateTcExposedMfgData(this.WeldContainer));
    }
    else
      operationCreation = TxApplication.ActiveDocument.CurrentOperation == null || !(TxApplication.ActiveDocument.CurrentOperation is ITxWeldOperation) ? (ITxWeldLocationOperationCreation) TxApplication.ActiveDocument.OperationRoot : (ITxWeldLocationOperationCreation) (TxApplication.ActiveDocument.CurrentOperation as ITxWeldOperation);
    TxWeldLocationOperationCreationData operationCreationData = new TxWeldLocationOperationCreationData();
    operationCreationData.WeldPointCreationData = pointCreationData;
    operationCreationData.Name = mfgName;
    TxWeldLocationOperation locationOperation = (TxWeldLocationOperation) null;
    if (operationCreation != null && operationCreation.CanCreateWeldLocationOperation(operationCreationData))
      locationOperation = operationCreation.CreateWeldLocationOperation(operationCreationData);
    if (!TxWeldLocationOperation.op_Inequality(locationOperation, (ITxObject) null) || part == null)
      return;
    TxComponent txComponent = part as TxComponent;
    if (TxComponent.op_Inequality(txComponent, (ITxObject) null))
      locationOperation.WeldPoint.LeadingPart = (ITxLeadingPart) txComponent;
    if (part is ITxLocatableObject itxLocatableObject)
      locationOperation.AttachTo(itxLocatableObject);
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) part);
    ((TxMfgFeature) locationOperation.WeldPoint).AssignParts(txObjectList);
  }

  public void Accept(string wpName, TxTransformation frameLocation, ITxObject part)
  {
    this.RemoveTemporaryFrame();
    if (TxTransformation.op_Equality(frameLocation, (TxTransformation) null))
      frameLocation = TxApplication.ActiveDocument.WorkingFrame;
    TxApplication.ActiveDocument.UndoManager.StartTransaction();
    this.CreateMfgByCoordinates(wpName, frameLocation, part as ITxProcessModelObject);
    TxApplication.ActiveDocument.UndoManager.EndTransaction();
    TxApplication.RefreshDisplay();
  }

  public void Cancel() => this.RemoveTemporaryFrame();

  public bool IsPart(ITxObject obj) => this.PartValidator.IsValidObject(obj, out string _);

  public void SetTemporaryFrame(TxTransformation frameLocation)
  {
    this.RemoveTemporaryFrame();
    this.m_tempFrame = TxApplication.ActiveDocument.PhysicalRoot.CreateFrame(new TxFrameCreationData("tempMfgFrame", frameLocation));
    TxApplication.RefreshDisplay();
  }

  public void RemoveTemporaryFrame()
  {
    if (!TxFrame.op_Inequality(this.m_tempFrame, (ITxObject) null))
      return;
    this.m_tempFrame.Delete();
  }
}
